﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SystemWFH.API.Application.Command.EquipmentCommand;
using SystemWFH.API.Application.Command.ProductionPlanningCommand;
using SystemWFH.API.DTO;
using SystemWFH.Domain;

namespace SystemWFH.API.Controllers
{
    /// <summary>
    /// 生产管理--API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    [AllowAnonymous]
    public class ProductionPlanningsController : ControllerBase
    {
        /// <summary>
        /// 中介者服务
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">服务</param>
        public ProductionPlanningsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 生产计划显示+查询+分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultAPI<PageDTO<ProductionPlanning>>> GetAsync([FromQuery] ProductionPlanningGetAllCommand productionPlanningGet)
        {
            ResultAPI<PageDTO<ProductionPlanning>> result = new ResultAPI<PageDTO<ProductionPlanning>>();
            try
            {
                result.Data = await mediator.Send(productionPlanningGet);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 生产计划反填
        /// </summary>
        /// <param name="productionPlanning"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultAPI<ProductionPlanning>> GetByIdAsync([FromQuery] ProductionPlanningFindCommand productionPlanning)
        {
            ResultAPI<ProductionPlanning> result = new ResultAPI<ProductionPlanning>();
            try
            {
                result.Data = await mediator.Send(productionPlanning);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 生产计划单删除
        /// </summary>
        /// <param name="deleteCommand">编号ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ResultAPI<int>> Deletes(ProductionPlanningDeleteCommand deleteCommand)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data = await mediator.Send(deleteCommand);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 生产计划状态修改
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultAPI<int>> EquipmentStateUpdate([FromQuery] ProductionPlanningSataeCommand contract)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data = await mediator.Send(contract);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 生产计划修改
        /// </summary>
        /// <param name="updateCommand"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultAPI<int>> UpdateAsync(ProductionPlanningUpdateCommand updateCommand)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data = await mediator.Send(updateCommand);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
